package cn.itcast.cookie;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieDemo03 extends HttpServlet {


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {


		//		1.输出网站所有商品
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();

		out.print("当前网站所有商品为:</br>");
		Map<String ,Book> map = Db.getAll();
		Set<Entry<String, Book>> entrySet = map.entrySet();
		for (Entry<String, Book> entry : entrySet) {
			Book book = entry.getValue();

			out.print("<a href='/day07/CookieDemo04?id="+book.getId()+"'>"+book.getName()+"</a><br/>");

		}

		//		2.显示曾经看过的商品记录
		out.print("</br>你曾经看过的商品:</br>");
		Cookie[] cookies = request.getCookies();

		for (int i = 0; cookies!=null &&i < cookies.length; i++) {
			if ("bookhistory".equals(cookies[i].getName()) &&  cookies[i] !=null) {
				String[] ids = cookies[i].getValue().split("\\,");

				for (String id : ids) {
					Map<String,Book> map2 = Db.getAll();

					Book book = map2.get(id);
					out.print(book.getName() +"</br>");

				}



			}


		}



	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}


}
class  Db{

	private   static LinkedHashMap<String ,Book> map = new LinkedHashMap<>();

	static{
		map.put("1", new Book("1","javaewb开发", "老张", "老张一本好书"));
		map.put("2", new Book("2","jdbc开发", "老张", "老张一本好书"));
		map.put("3", new Book("3","spring开发", "老黎", "老黎一本好书"));
		map.put("4", new Book("4","struts开发", "老毕", "老毕一本好书"));
		map.put("5", new Book("5","android开发", "老黎", "老黎一本好书"));


	}


	public static Map  getAll(){
		return map;
	}

}

class Book{

	private String id;
	private String name;
	private String  author;
	private String description;



	public Book() {
		super();

	}
	public Book(String id, String name, String author, String description) {
		super();
		this.id = id;
		this.name = name;
		this.author = author;
		this.description = description;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}



}














